<template>
  <header class="header">
    <div class="header-left">
      <h1 class="logo">投产演练平台</h1>
    </div>
    <div class="header-right">
      <div class="user-menu-container">
        <!-- 用户菜单按钮 - 显示动态用户名 -->
        <button 
          class="user-menu-button"
          @click="toggleUserMenu"
          aria-label="用户菜单"
        >
          <div class="user-avatar">
            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg>
          </div>
          <span>{{ currentUser.username }}</span>
          <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="chevron"><polyline points="6 9 12 15 18 9"></polyline></svg>
        </button>
        
        <!-- 用户菜单弹出层 - 显示用户详细信息 -->
        <div v-if="isMenuOpen" class="user-dropdown">
          <div class="dropdown-header">
            <div class="user-info">
              <div class="user-name">{{ currentUser.username }}</div>
              <div class="user-role">{{ currentUser.role }}</div>
            </div>
          </div>
          
          <!-- 用户信息区域 -->
          <div class="user-details">
            <div class="user-detail-item">
              <label>邮箱</label>
              <span>{{ currentUser.email }}</span>
            </div>
            <div class="user-detail-item">
              <label>密码</label>
              <span>{{ maskPassword(currentUser.password) }}</span>
            </div>
          </div>
          
          <div class="dropdown-divider"></div>
          
          <!-- 红色的退出登录按钮 -->
          <div class="logout-button" @click="handlelogout">
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path><polyline points="16 17 21 12 16 7"></polyline><line x1="21" y1="12" x2="9" y2="12"></line></svg>
            <span>退出登录</span>
          </div>
        </div>
      </div>
    </div>
  </header>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const isMenuOpen = ref(false);

// 从localStorage获取当前用户信息
const currentUser = computed(() => {
  const userData = localStorage.getItem('user');
  if (userData) {
    try {
      const user = JSON.parse(userData);
      // 提供默认值，以防数据不完整
      return {
        username: user.username || '管理员',
        email: user.email || 'example@company.com',
        password: user.password || '',
        role: user.role || '系统管理员'
      };
    } catch (error) {
      console.error('解析用户数据失败:', error);
    }
  }
  // 返回默认用户信息
  return {
    username: '管理员',
    email: 'example@company.com',
    password: '',
    role: '系统管理员'
  };
});

// 切换菜单显示状态
const toggleUserMenu = () => {
  isMenuOpen.value = !isMenuOpen.value;
};

// 密码掩码函数
const maskPassword = (password) => {
  if (!password) return '********';
  return '*'.repeat(password.length);
};

// 退出登录处理
const handlelogout = () => {
  // 清除用户登录状态
  localStorage.removeItem('user');
  // 跳转到登录页面
  router.push('/login');
};
</script>

<style scoped>
.header {
  height: 64px;
  background-color: var(--primary-color);
  color: #ffffff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 var(--spacing-xl);
  box-shadow: var(--shadow-md);
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(4px);
  background-image: linear-gradient(135deg, var(--primary-color), var(--primary-hover));
}

.logo {
  font-size: var(--font-size-lg);
  font-weight: 700;
  margin: 0;
  color: #ffffff;
  letter-spacing: 0.05em;
  transition: all 0.3s ease;
}

.logo:hover {
  transform: scale(1.02);
}

.header-right {
  display: flex;
  align-items: center;
  gap: var(--spacing-md);
}

/* 用户菜单按钮 */
.user-menu-container {
  position: relative;
}

.user-menu-button {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  padding: var(--spacing-sm) var(--spacing-md);
  border: none;
  border-radius: var(--border-radius-base);
  background-color: rgba(255, 255, 255, 0.1);
  color: white;
  font-size: var(--font-size-base);
  cursor: pointer;
  transition: all 0.3s ease;
  backdrop-filter: blur(2px);
}

.user-menu-button:hover {
  background-color: rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
}

.user-avatar {
  background-color: rgba(255, 255, 255, 0.2);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.user-menu-button:hover .user-avatar {
  background-color: rgba(255, 255, 255, 0.3);
  transform: scale(1.1);
}

.chevron {
  transition: transform 0.3s ease;
}

.user-menu-button:hover .chevron {
  transform: rotate(180deg);
}

/* 下拉菜单样式 */
.user-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: var(--spacing-sm);
  min-width: 240px;
  background-color: white;
  border-radius: var(--border-radius-md);
  box-shadow: var(--shadow-lg);
  z-index: 1000;
  overflow: hidden;
  transform-origin: top right;
  transform: scale(0.95);
  opacity: 0;
  animation: dropdownFadeIn 0.2s ease forwards;
}

@keyframes dropdownFadeIn {
  to {
    transform: scale(1);
    opacity: 1;
  }
}

.dropdown-header {
  padding: var(--spacing-lg);
  background-color: var(--bg-light);
  border-bottom: 1px solid var(--border-light);
}

.user-info {
  text-align: center;
}

.user-name {
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 2px;
  font-size: var(--font-size-md);
}

.user-role {
    color: var(--text-secondary);
    font-size: var(--font-size-xs);
    background-color: var(--bg-white);
    padding: 2px 8px;
    border-radius: 10px;
    display: inline-block;
  }

  /* 用户详细信息区域 */
  .user-details {
    padding: var(--spacing-lg);
    background-color: var(--bg-white);
  }

  .user-detail-item {
    display: flex;
    justify-content: space-between;
    margin-bottom: var(--spacing-sm);
    padding: var(--spacing-xs) 0;
    transition: all 0.2s ease;
  }

  .user-detail-item:hover {
    background-color: var(--bg-light);
    padding: var(--spacing-xs) var(--spacing-sm);
    border-radius: var(--border-radius-sm);
  }

  .user-detail-item label {
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    font-weight: 500;
  }

  .user-detail-item span {
    color: var(--text-primary);
    font-size: var(--font-size-sm);
    font-weight: 500;
  }

  .dropdown-divider {
    height: 1px;
    background-color: var(--border-light);
    margin: 4px 0;
  }

  /* 红色的退出登录按钮 */
  .logout-button {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--spacing-xs);
    padding: var(--spacing-md);
    background-color: var(--danger-color);
    color: white;
    cursor: pointer;
    transition: all 0.3s ease;
    border: none;
    width: 100%;
    font-size: var(--font-size-base);
    font-weight: 500;
  }

  .logout-button:hover {
    background-color: #f87171;
    transform: translateY(-1px);
  }

  .logout-button:active {
    transform: translateY(0);
  }

.logout-button:hover {
  background-color: #c53139;
}
</style>